package net.asgarli.ivault.api;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


/**
 * Class the indicates the version of something. 
 * Contains major and minor versions. For example 2.11 where 2 will be major version and 11 minor version.
 * 
 * @author Elchin Asgarli
 * @since 0.1
 * @version 0.1
 */
public class VersionInfo implements IVSerializable {

	private static final long serialVersionUID = 1L;

	/** Major version */
	private int majorVersion;

	/** Minor version */
	private int minorVersion;

	/**
	 * Constructor that takes major and minor versions as parameter.
	 * @param majorVersion
	 * @param minorVersion
	 */
	public VersionInfo(int majorVersion, int minorVersion) {
		this.majorVersion = majorVersion;
		this.minorVersion = minorVersion;
	}

	public int getMajorVersion() {
		return majorVersion;
	}

	public int getMinorVersion() {
		return minorVersion;
	}	

	/**
	 * Returns the String representation of object in format of MA.MI where
	 * MA is major version and MI is minor version and there is a dot between them.
	 */
	@Override
	public String toString() {
		return majorVersion + "." + minorVersion;
	}

	/** {@inheritDoc} */
	@Override
	public void restoreFromByteArray(byte[] bytes) throws IOException {
		ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
		DataInputStream in = new DataInputStream(bis);
		majorVersion = in.readInt();
		minorVersion = in.readInt();
	}

	/** {@inheritDoc} */
	@Override
	public byte[] saveToByteArray() throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);
		dos.writeInt(majorVersion);
		dos.writeInt(minorVersion);
		dos.flush();
		dos.close();
		bos.close();
		return bos.toByteArray();
	}		
}
